<?php
namespace Idiom\Controller;
use  Common\Controller\IdiomController;
//活动
class ActivityController extends IdiomController {

    public function __construct()
    {
        $sessionid = 'PHPSESSID='.$this->getSessionId();

        $getsessionid = $_SERVER['HTTP_COOKIE'];
        if ($sessionid!=$getsessionid){
            $this->returnData('','状态已过期请重新授权',400);
        }
    }

    /**
     * 获取挑战机会
     */
    public function getopportunity(){

        $member_id = I('post.member_id','','int');

        if (!$member_id){
            $this->returnData('','参数错误',401);
        }

        $date = date('Y-m-d',time());

        $where['idiom_member_id'] = $member_id;
        $where['signin_time'] = $date;

        $issign="";
        $issign = M('idiom_signin')->where($where)->getField('signin_num'); //判断今天有没有签到
        if (!$issign||$issign==""){
            $data = array(
                'idiom_member_id'=>$member_id,
                'signin_time'=>$date,
                'signin_num'=>'1'
            );
            M('idiom_signin')->data($data)->add();
            $giveopportunity = M('idiom_rule')->where(array('category'=>'idiom'))->getField('giveopportunity');
            if (empty($giveopportunity)){
                $giveopportunity = 2;
            }
            M('idiom_member')->where(array('idiom_member_id'=>$member_id))->save(array('giveopportunity'=>$giveopportunity)); //每天赠送机会
        }

        $opportunity = M('idiom_member')->field('opportunity,giveopportunity')->where(array('idiom_member_id'=>$member_id))->find();//获取挑战机会

        $chang = $opportunity['opportunity']+$opportunity['giveopportunity'];
        if ($chang>0){
            if ($opportunity['giveopportunity']>0){
                M('idiom_member')->where(array('idiom_member_id'=>$member_id))->setDec('giveopportunity'); //先减去赠送的
            }else{
                M('idiom_member')->where(array('idiom_member_id'=>$member_id))->setDec('opportunity'); //挑战机会减一
            }

            M('idiom_member')->where(array('idiom_member_id'=>$member_id))->setInc('number'); //挑战次数加一
            $this->returnData(array('opportunity'=>$chang),'成功',200);
        }

        $this->returnData(array('opportunity'=>$chang),'没有挑战机会',101);

    }

    /**
     * 获取分享内容模板
     */

    public function getsharecontent(){


//        $data = array(
//            'title'=>'金主爸爸叫你来领现金！',
//            'desc'=>'金主爸爸叫你来领现金！',
//            'imgUrl'=>'http://oss.beikg.cn/share/idiomshare.png',
//        );

        $data = array(
            'title'=>'挑战你的脑力极限！！！',
            'desc'=>'挑战你的脑力极限！！！',
            'imgUrl'=>'http://oss.beikg.cn/share/share.jpg',
        );

        $this->returnData($data);
    }


    /**
     * 微信群分享记录
     */
    public function sharerecord(){

        $member_id = I('post.member_id','','int');
        $shareType = I('post.shareType','2','int');
        $encryptedData = $_POST['encryptedData'];
        $iv = $_POST['iv'];


        if (!$encryptedData||!$member_id||!$iv){
            $this->returnData('','参数错误',401);
        }

        $info = M('idiom_member')->field('appid,session_key')->where(array('idiom_member_id'=>$member_id))->find();

        $appid = $info['appid'];
        $sessionkey = $info['session_key'];

        $openGId = $this->getopenGId($appid,$sessionkey,$encryptedData,$iv);

        if ($openGId=='-1'){
            $this->returnData('','分享失败',100);
        }

        $time = date('Y-m-d',time());

        $where['sharetime'] = $time;
        $where['idiom_member_id'] = $member_id;
        $where['openGId'] = $openGId;

        if ($shareType=='1'){
            $interior = $record = M('idiom_interior_group')->where($where)->getField('sharenum'); //判断是否已分享
            if ($interior){
                M('idiom_interior_group')->where($where)->setInc('sharenum');
                $this->returnData(array('repetition'=>'yes'),'今天已分享',201);
            }
            //如果没分享过则插入
            $data = array(
                'openGId'=>$openGId,
                'sharetime'=>$time,
                'sharenum'=>1,
                'idiom_member_id'=>$member_id,
            );

            M('idiom_interior_group')->data($data)->add();
            $this->returnData(array('repetition'=>'no'),'立即复活',200);
        }

        $where2['sharetime'] = $time;
        $where2['idiom_member_id'] = $member_id;

        $count = M('idiom_wechat_group')->where($where2)->count();

        $maxshare = M('idiom_rule')->where(array('category'=>'idiom'))->getField('maxshare');
        if (empty($maxshare)){
            $maxshare = 7;
        }

        if ($count>=$maxshare){
            $this->returnData(array('repetition'=>'out'),'一天有效分享次数为10',201);
        }


        $record = "";
        $record = M('idiom_wechat_group')->where($where)->getField('sharenum'); //判断是否已分享

        //如果已分享则自增分享次数然后返回
        if ($record){
            M('idiom_wechat_group')->where($where)->setInc('sharenum');

            $this->returnData(array('repetition'=>'yes'),'今天已分享',201);
        }


        //如果没分享过则插入，用户挑战机会加一
        $data = array(
            'openGId'=>$openGId,
            'sharetime'=>$time,
            'sharenum'=>1,
            'idiom_member_id'=>$member_id,
        );

        M('idiom_wechat_group')->data($data)->add();

        M('idiom_member')->where(array('idiom_member_id'=>$member_id))->setInc('opportunity'); //挑战机会加一

        $opportunity = M('idiom_member')->field('opportunity,giveopportunity')->where(array('idiom_member_id'=>$member_id))->find();//获取挑战机会

        $chang = $opportunity['opportunity']+$opportunity['giveopportunity'];

        $this->returnData(array('repetition'=>'no','opportunity'=>$chang),'分享成功',200);
    }



    /**
     * 规则
     */
    public function setrule(){

        $data  = array(
            '1.每人每天有数次挑战机会。',
            '2.排行榜每周更新。',
            '3.端午活动圆满结束。更多模式，敬请期待！',
            '4.打开客服窗口，回复1，获取详情'
        );

        $this->returnData($data);

    }




    /**
     * 获取题目进行挑战
     */

    public function challenge(){

        $member_id = I('post.member_id','','int');

        $opportunity = I('post.opportunity','','int');

        $state = I('post.state','','int');

        if ($member_id==""||$opportunity==""||$opportunity<1||!$state){
            $this->returnData('','没有挑战机会',101);
        }

        //首次获取
        if ($state=='1'){
            $member = M('idiom_member')->field('nickName,avatarUrl')->where(array('idiom_member_id'=>$member_id))->find();
            $year = (int)date('Y');
            $week = (int)date('W');
            $insert = array(
                'date'=>date('Y-m-d'),
                'customs'=>'0',
                'idiom_member_id'=>$member_id,
                'nickname'=>$member['nickname'],
                'avatarurl'=>$member['avatarurl'],
                'year'=>$year,
                'week'=>$week,
            );
            $insertid = M('idiom_chang')->data($insert)->add();
            $ext['chang_id'] = $insertid;
            //获取随机值
            $idiomquersion = $this->getidiomquersion(50);
            foreach ($idiomquersion as $key=>$val){
                if ($key<50){
                    $idiomquersion[$key]['time'] = 10;
                }
                if ($key<40){
                    $idiomquersion[$key]['time'] = 15;
                }
                if ($key<30){
                    $idiomquersion[$key]['time'] = 20;
                }
                if ($key<20){
                    $idiomquersion[$key]['time'] = 25;
                }
                if ($key<10){
                    $idiomquersion[$key]['time'] = 30;
                }

            }
        }else{
            $idiomquersion = $this->getidiomquersion(50);

            foreach ($idiomquersion as $ke=>$va){

                $idiomquersion[$ke]['time'] = 10;
            }
        }


        if ($idiomquersion){
            $sharelive = M('idiom_rule')->where(array('category'=>'idiom'))->getField('sharelive');
            $data['question'] = $idiomquersion;
            $sharelive = 1;//分享复活
            $ext['sharelive'] = $sharelive;
            $ext['livecount'] = 2;

            $data['ext'] = $ext;

            $this->returnData($data,'成功',200);
        }else{
            $this->returnData('','获取题目失败',100);
        }


    }

    /**
     * 答对一题
     */
    public function inanwser(){

        $chang_id = I('post.chang_id','','int');
        $member_id = I('post.member_id','','int');
        $state = I('post.state','','int');


        if (!$chang_id||!$member_id||!$state){
            $this->returnData('','非法操作',401);
        }



        $where['idiom_chang_id'] = $chang_id;
        $where['idiom_member_id'] = $member_id;

        if ($state=='2'){
            $where1['idiom_member_id'] = $member_id;
            $where1['date'] = date('Y-m-d');
            $maxcustoms = M('idiom_chang')->where($where1)->max('customs');
            $nowcustoms = M('idiom_chang')->where($where)->getField('customs');
            $data = array(
                'maxcustoms'=>$maxcustoms,
                'nowcustoms'=>$nowcustoms,
            );
            $this->returnData($data);
        }

       $id = M('idiom_chang')->where($where)->setInc('customs');

        if ($id){
            $this->returnData('','成功',200);
        }
        $this->returnData('','失败',100);

    }

    /**
     * 排行
     */
    public function rank(){

        $state = I('post.state','','int');

        if ($state=='1'){ //今日排行
           // $date = date('Y-m-d');
            //$rank = $this->getrank($date,$state);

           // $rank = $this->getweekrank();
            $rank = $this->getcacheweek2();
            $tabName_1 = '本周排行';
            $tabName_2 = '端午得奖公示';
            $msg['tabName_1'] = $tabName_1;
            $msg['tabName_2'] = $tabName_2;
            $this->returnData($rank,$msg);
        }

        if ($state=='2'){ //近期得奖
            $rank = $this->gift($state);
            $tabName_2 = '端午得奖公示';
            $this->returnData($rank,$tabName_2);
        }


    }








}